package ls.vuecp.gui.toolbar;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

import tufts.vue.VUE;
import tufts.vue.VueResources;


public class VueToolbarController 
{
	private static VueToolbarController mthis = null;
	public static VueToolbarController getController() {
		if( mthis == null )	new VueToolbarController();	return mthis; }
	public static final String DefaultToolsKey = 
			VUE.isApplet() ? "appletToolbarToolNames" : "mainToolbarToolNames";
    
	//
	private VueToolPanel mToolPanel = null;	// main toolbar panel;
	private JPanel toolbarPanel = null;		// top toolbar panel;
	
	//private VueTool[] mVueTools = null;
	private Vector<VueTool> mVueTools = new Vector<VueTool>();
	private int[] separatorPositions = null;
	private VueTool mSelectedTool = null;	
	
	private VueTool selectionTool;
	private VueTool nodeModeTool;
	private VueTool linkModeTool;
	private VueTool rapidLinkModeTool;
	private VueTool richTextTool;
	private VueTool zoomTool;
	private VueTool handTool;
	private VueTool launchPathwayTool;
	
	private VueTool testTool;
	
	public VueToolbarController()
	{
		mthis = this;
	}
	//private String[] mToolNames = new String[10];
	public void buildToolbar()
	{
		testTool = new TestTool();
		VueToolUtils.setToolProperties(testTool, "testTool");
		
		buildTools();
		
		mToolPanel = new VueToolPanel();
		
		
		mToolPanel.addTool(selectionTool,true);
		mToolPanel.addTool(nodeModeTool,true);
		mToolPanel.addTool(linkModeTool,true);
		mToolPanel.addTool(rapidLinkModeTool,true);
		mToolPanel.addTool(richTextTool,true);
		
		mToolPanel.addSeparator();
		mToolPanel.addTool(zoomTool,true);
		mToolPanel.addTool(handTool,true);
		
		mToolPanel.addTool(testTool,true);
		
		toolbarPanel = new JPanel();
		constructToolPanel(mToolPanel);
	}
	
	private void buildTools()
	{
		//String[] names = VueResources.getStringArray(DefaultToolsKey);
		selectionTool = new VueTool();
		VueToolUtils.setToolProperties(selectionTool, "selectionTool");
		selectionTool.setSelectedSubTool(selectionTool.getSubTool("arrowTool"));
		nodeModeTool = new VueTool();
		VueToolUtils.setToolProperties(nodeModeTool, "nodeModeTool");
		linkModeTool = new VueTool();
		VueToolUtils.setToolProperties(linkModeTool, "linkModeTool");
		rapidLinkModeTool = new VueTool();
		VueToolUtils.setToolProperties(rapidLinkModeTool, "rapidLinkModeTool");
		richTextTool = new VueTool();
		VueToolUtils.setToolProperties(richTextTool, "richTextTool");
		zoomTool = new VueTool();
		VueToolUtils.setToolProperties(zoomTool, "zoomTool");
		handTool = new VueTool();
		VueToolUtils.setToolProperties(handTool, "handTool");
		launchPathwayTool = new VueTool();
		VueToolUtils.setToolProperties(launchPathwayTool,"launchPathwayTool");
		
		//mVueTools = VueToolUtils.loadTools(DefaultToolsKey);
		mVueTools.add(selectionTool);
		mVueTools.add(nodeModeTool);
		mVueTools.add(linkModeTool);
		mVueTools.add(rapidLinkModeTool);
		mVueTools.add(richTextTool);
		
		mVueTools.add(zoomTool);
		mVueTools.add(handTool);
		
		mVueTools.add(launchPathwayTool);
	}
	private void buildToolPanel()
	{
		
	}
	private JPanel constructToolPanel(JComponent toolbar)
	{
		//JPanel toolbarPanel = new JPanel();
    	toolbarPanel.setLayout(new GridBagLayout());
        FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
        flowLayout.setVgap(0);
        //toolbarPanel.
        //toolbarPanel.setLayout(flowLayout);
        GridBagConstraints gBC = new GridBagConstraints();

        gBC.fill = GridBagConstraints.NONE;			
		gBC.gridx = 0;
		gBC.gridy = 0;
		gBC.weightx = 0.0;
		gBC.insets = new Insets(0, 0, 0, 0);
		//add(searchResultTbl, gBC);
        //toolbarPanel.add(returnToMapButton,gBC);
        toolbarPanel.add(toolbar,gBC);

//        gBC.fill = GridBagConstraints.NONE;			
//        gBC.gridx = 1;
//        gBC.gridy = 0;
//        gBC.weightx = 0.0;
//        gBC.insets = new Insets(0, 0, 0, 0);
//        
        return toolbarPanel;
	}
	
	
	public void handleToolSelection(VueTool selectedSubTool) {
		// TODO Auto-generated method stub
		
	}
	
	static public void destroyController()
	{
		mthis = null;
	}
	
	public VueToolPanel getToolbar() {
        return mToolPanel;
    }
	public Vector<VueTool> getTopLevelTools() {
        return mVueTools;
    }
	public JPanel getToolbarPanel() { 
		return toolbarPanel;
		//return mToolPanel;
	}
	static public VueTool getActiveTool() {
        if (mthis == null)
            return null;
        else
            return mthis.getSelectedTool();
    }
	public VueTool getSelectedTool() {
        if (getToolbar() == null)
            return null;
        else
            return getToolbar().getSelectedTool();
    }
	public JPanel getSuggestedContextualPanel() { return null; }
	
}
